--[[
--取pb_list内所有pb文件名
function GetProtoList(protoTitle, tableList)
    local pbList = {}
    for _, v in pairs(tableList) do
        table.insert(pbList, protoTitle..v..".proto")
    end

    return pbList
end

function LoadPb(node)
	local protoTitle = skynet.getenv("proto")
	local pbList = GetProtoList(protoTitle, g_pbList[node])
	log.info("pbList=", pbList)
	for _, v in pairs(pbList) do
		assert( protoc:loadfile(v) )
	end	
	--log.info("protoc.path=", protoc.paths)
	--log.info("protoc=", protoc)
end
--]]

--加载pb文件
function PbLoad(protoTitle, file)
    local oneProto = protoTitle..file..".proto"
    assert( protoc:loadfile(oneProto) )
    log.info("LoadOnePb oneProto=", oneProto)
end

--解包
function PbDecode(node, rawData)
    --log.info("rawData=", rawData)
    --string:byte(x):返回string第几位的ascii编码
    local packId = rawData:byte(1) + rawData:byte(2) * 256  
    local packName = g_packType[node][packId]
    if not packName then
        local fileName = ""
        for k, v in pairs(g_packId) do
            if v==packId then 
                fileName = k
                break 
            end
        end
        log.error("PbDecode g_packType[...] have no file: ", fileName.."--->node="..node)
        return 
    end
    --log.info("rawData=", rawData) 
    --log.info("rawData:byte1=", rawData:byte(1)) 
    --log.info("rawData:byte2=", rawData:byte(2)) 
   
    --sting:sub(x): 从第几位截取字符串 (1.2位表示包头地址，第3位开始为数据)
    local msgData = rawData:sub(3)
	local data, error = pb.decode(packName..".MsgBody", msgData)
	--log.info("data=", data)
    --log.info("error=", error)
    return packId, data 
end

--打包
function PbEncode(msgName, msgBody, packId)
    --log.info("msgName=", msgName)
    --log.info("msgBody=", msgBody)
    --protoc:loadfile("hand_hall.proto")
	local msgBuf = pb.encode(msgName, msgBody)
	if  #msgBuf == 0 then
		skynet.error("msgBuf =", msgBuf)
		return
	end

	--http://cloudwu.github.io/lua53doc/manual.html#6.4.2
    local encodeMsg = string.pack("=H", packId) .. msgBuf
    if not encodeMsg then
        skynet.error('msg pack failed' .. msgBuf)
        return
    end

    return encodeMsg
end